﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Text;

namespace ProjectStudy
{
    public partial class 发送邮件找回密码 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //sendMail("这是邮件主题", "", "这是主要内容", "984779688@qq.com");
            

            //MailMessage message = new MailMessage();
            //  //设置发件人,发件人需要与设置的邮件发送服务器的邮箱一致
            //  MailAddress fromAddr = new MailAddress("940271306@qq.com");

            // message.From = fromAddr;
            // //设置收件人,可添加多个,添加方法与下面的一样
            // message.To.Add("cxf007@163.com");
            // //设置抄送人
            // message.CC.Add("cxf6910697@163.com");
            // message.CC.Add("940271306@qq.com");
            // message.CC.Add("984779688@qq.com");
            // //设置邮件标题
            // message.Subject = "来自深圳的票漂流瓶";
            // //设置邮件内容
            // message.Body = "来自深圳的票漂流瓶";
            // //设置邮件发送服务器,服务器根据你使用的邮箱而不同,可以到相应的 邮箱管理后台查看,下面是QQ的
            // SmtpClient client = new SmtpClient("smtp.qq.com", 25);
            // //设置发送人的邮箱账号和密码
            // client.Credentials = new NetworkCredential("940271306@qq.com", "utukuarivitxbbbf");
            // //启用ssl,也就是安全发送
            // client.EnableSsl = true;
            // //发送邮件
            // try
            // {
            //     client.Send(message);
            // }
            // catch (Exception e1)
            // { 
                
            // }
        }

        public bool SendEmail(List<string> receivers,List<string> cc, string subject,string body,List<string> filePath)
        {
            string sender = "940271306@qq.com";
            string pwd = "utukuarivitxbbbf";

            MailMessage msg = new MailMessage();
            string server = "smtp." + sender.Split('@')[1];

            //收件人地址
            if (receivers != null && receivers.Count > 0)
            {
                foreach (string r in receivers)
                {
                    msg.To.Add(r);
                }
            }
            //抄送人地址
            if (cc != null && cc.Count > 0)
            {
                foreach (string r in cc)
                {
                    msg.CC.Add(r);
                }
            }

            msg.From = new MailAddress(sender, "Fly");//发件人邮箱，名称 

            msg.Subject = subject;
            msg.SubjectEncoding = Encoding.UTF8;//标题格式为UTF8 

            msg.Body = body;
            msg.BodyEncoding = Encoding.UTF8;//内容格式为UTF8
            if (filePath != null && filePath.Count > 0)
            {
                foreach (string f in filePath)
                {
                    try
                    {
                        msg.Attachments.Add(new Attachment(f));
                    }
                    catch(Exception)
                    {
                        throw new Exception("附件添加失败,请检查附件");
                    }
                }
            }
            msg.Priority = MailPriority.High;//设置邮件优先级

            SmtpClient client = new SmtpClient();

            client.Host = server;//SMTP服务器地址 
            client.Port = 25;//SMTP端口，QQ邮箱填写25 

            client.EnableSsl = true;//启用SSL加密 
            //发件人邮箱账号，授权码(注意此处，是授权码你需要到qq邮箱里点设置开启Smtp服务，然后会提示你第三方登录时密码处填写授权码)
            client.Credentials = new System.Net.NetworkCredential(sender, pwd);

            try
            {
                client.Send(msg);//发送邮件
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<string> receivers = new List<string>();
            receivers.Add("cxf6910697@163.com");
            List<string> file = new List<string>();
            file.Add("D://1.zip");
            bool s = SendEmail(receivers, null, "深圳简介嗯呢", "这是深圳简介", file);
            Label1.Text = s == true ? "发送成功" : "发送失败";
        }
    }
}